package game.metalmax.ui
{
	import flash.display.DisplayObjectContainer;
	
	import game.metalmax.controller.Notifications;
	import game.metalmax.module.world.WorldManager;
	import game.metalmax.ui.main.DialogWindow;
	import game.metalmax.ui.main.MainWindow;
	
	import showtime.framework.manager.WindowManager;
	import showtime.framework.mvc.interfaces.INotification;
	import showtime.framework.mvc.patterns.mediator.Mediator;

	/**
	 * WindowMediator.as
	 * Yin Hao
	 * @version 1.0.0
	 * 
	 * Description.
	 * 
	 * Copyright (c) 2012 Yin Hao
	 * 
	 * Permission is hereby granted, free of charge, to any person obtaining a copy
	 * of this software and associated documentation files (the "Software"), to deal
	 * in the Software without restriction, including without limitation the rights
	 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	 * copies of the Software, and to permit persons to whom the Software is
	 * furnished to do so, subject to the following conditions:
	 * 
	 * The above copyright notice and this permission notice shall be included in
	 * all copies or substantial portions of the Software.
	 * 
	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	 * THE SOFTWARE.
	 */
	public class WindowLayerMediator extends Mediator
	{
		private var parent:Object = null;
		
		public function WindowLayerMediator(parent:Object)
		{
			super(String(WindowLayerMediator));
			
			this.parent = parent;
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
			viewComponent = new WindowLayer();
			parent.addChild(viewComponent);	
			
		}
		
		override public function onRemove():void
		{
			super.onRemove();
		}
		
		override public function listNotificationInterests():Array
		{
			return [Notifications.OPEN_DIALOG_WINDOW, Notifications.CLOSE_DIALOG_WINDOW, Notifications.OPEN_MAIN_WINDOW];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			sendNotification(Notifications.DEACTIVATE_ZONE);
			
			switch (notification.getName())
			{
				case Notifications.OPEN_DIALOG_WINDOW:
				{
					openDialogWindow(notification.getBody());
					break;
				}
				case Notifications.CLOSE_DIALOG_WINDOW:
				{
					closeDialogWindow(notification.getBody());
					break;
				}
				case Notifications.OPEN_MAIN_WINDOW:
				{
					openMainWindow(notification.getBody());
					break;
				}					
					
			}	
			
		}
		
		private function openDialogWindow(data:Object = null):void
		{
			WindowManager.centerPopUp(WindowManager.createPopUp(DialogWindow,DisplayObjectContainer(viewComponent), true)).refresh(data);
			
		}
		
		private function closeDialogWindow(data:Object = null):void
		{
			WorldManager.getInstance().restoreLastAccessCharacterSOBStatus();
			sendNotification(Notifications.ACTIVE_ZONE);
			WindowManager.removePopUp(DialogWindow);			
		}
		
		private function openMainWindow(data:Object = null):void
		{
			WindowManager.centerPopUp(WindowManager.createPopUp(MainWindow,DisplayObjectContainer(viewComponent), true)).refresh(data);
		}
	}
}